// pages/shoppingCart/shoppingCart.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    fullHeight: app.globalData.fullcontain,
    navmess:{
      navtitle: "购物车",
      backhide: 0,
      topheight: 1
    },
    readyChoose: false,
    count: 0,   //合计
    number: 0,  //件数
    demolist:[
      {
        "imgUrl": "./images/goodimg.png",
        "tit": "日本贴片美甲",
        "price": "89.10",
        "num": "0",
        "selected": false
      },
      {
        "imgUrl": "./images/goodimg.png",
        "tit": "日本贴片美甲",
        "price": "89.10",
        "num": "0",
        "selected": false
      },
      {
        "imgUrl": "./images/goodimg.png",
        "tit": "日本贴片美甲",
        "price": "89.10",
        "num": "0",
        "selected": false
      },
      {
        "imgUrl": "./images/goodimg.png",
        "tit": "日本贴片美甲",
        "price": "89.10",
        "num": "0",
        "selected": false
      },
      {
        "imgUrl": "./images/goodimg.png",
        "tit": "日本贴片美甲",
        "price": "89.10",
        "num": "0",
        "selected": false
      },
    ]
  },
  bindMinus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index),
        num = this.data.demolist[index].num,
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
    if(demolist[index].selected && num>0){
      num--;
      this.setData({
        count: this.data.count - 1 * price,
        number: this.data.number - 1
      });
    }else if(num>0){
      num--;
    }
    demolist[index].num = num;
    this.setData({
      demolist: demolist
    });
  },
  bindPlus: function (e) {
    var index = parseInt(e.currentTarget.dataset.index),
        num = this.data.demolist[index].num,
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
    if(demolist[index].selected){
      num++;
      this.setData({
        count: this.data.count + 1 * price,
        number: this.data.number + 1
      });
    }else{
      num++;
    }
    demolist[index].num = num;
    this.setData({
      demolist: demolist
    });
  },
  bindCheckbox: function (e) {
    var index = parseInt(e.currentTarget.dataset.index), 
        selected = this.data.demolist[index].selected,
        num = parseInt(this.data.demolist[index].num),
        price = this.data.demolist[index].price;
    var demolist = this.data.demolist;
        demolist[index].selected = !selected;
    this.setData({
      demolist: demolist
    });
    if (!selected) {
      this.setData({
        count: this.data.count + num * price,
        number: this.data.number + num 
      });
    } else {
      this.setData({
        count: this.data.count - num * price,
        number: this.data.number - num
      });
    }
  },
  // 删除checkbox
  delclick: function(){
    this.setData({
      readyChoose: !this.data.readyChoose
    })
  },
  // 多选删除事件
  del: function(e) {
    var that = this;
    
    console.log("删除开关",that.data.readyChoose)
    if(that.data.readyChoose){
      wx.showModal({
        title: '提示',
        content: '确认要删除多条信息么？',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            var demolist = that.data.demolist;
            for (var i = demolist.length - 1; i >=0; i--) {
                if(demolist[i].selected){
                  demolist.splice(i,1);
                }
            }
            that.setData({
              demolist: that.data.demolist
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})